<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;
use DB;

class AccountController extends BaseController
{
    public function login(Request $request)
    {
        if ($request->isMethod('post')) {
            $username = $request->input('username', '');
            $password = md5($request->password);
            $admin = DB::table('admin')->where('username', $username)->where('password', $password)->where('status', '<>', 99)->first();
            if (empty($admin)) return jsonFailed('用户名或密码错误');
            if ($admin->status == 0) return jsonFailed('该账号已关闭');
            $data = [];
            $data['admin_id'] = $admin->id;
            $data['ip'] = Request()->ip();
            $data['token'] = md5($request->password . $admin->id . time() . rand(1000, 9999));
            DB::table('admin_login_log')->insert($data);
			$admin->token = $data['token'];
            session(['admin' => object_to_array($admin)]);
            return arraySuccess();
        }
        return view('admin.account.login');
    }

    public function logout()
    {
        $admin = session('admin');
        DB::table('admin_login_log')->where('admin_id', $admin['id'])->update(['status' => 0]);
        session()->pull('admin', null);
        return jsonSuccess();
    }
}
